package com.sunshine.ssm.pool;

import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;

import java.util.UUID;

/**
 * @author WeiHong
 * @date 2019/1/11
 */
public class PersonFactory extends BasePooledObjectFactory<Person> {



    @Override
    public Person create() {
        //try {
        //    Thread.sleep(1000);
        //} catch (InterruptedException e) {
        //    e.printStackTrace();
        //}
        Person p = new Person();
        p.setName(UUID.randomUUID().toString());
        System.out.println("创造了一个人：" + p.getName());
        return p;
    }

    @Override
    public PooledObject<Person> wrap(Person obj) {
        return new DefaultPooledObject<>(obj);
    }

    /**
     * 验证对象
     */
    @Override
    public boolean validateObject(PooledObject<Person> p) {
        System.out.println("验证连接");
        //可获取到对象
        Person object = p.getObject();
        return true;
    }
}
